package struct;

import com.alibaba.fastjson.JSONObject;
import struct.enums.CheckResult;

public class CheckInfo {
    public static final String INV_NAME_FIELD = "name";
    public static final String LINE_NUMBER_FIELD = "line_number";
    public static final String ITER_ID_FIELD = "iter_id";
    public static final String CHECK_TIME_FIELD = "check_time";
    public static final String INV_RESULT_FIELD = "result";

    String name;
    int lineNumber;
    int iterId;
    long checkTime;
    CheckResult result;

    public CheckInfo(String name, int lineNumber, int iterId, long checkTime, CheckResult result) {
        this.name = name;
        this.lineNumber = lineNumber;
        this.iterId = iterId;
        this.checkTime = checkTime;
        this.result = result;
    }

    @Override
    public String toString() {
        JSONObject infoJson = new JSONObject();
        infoJson.put(INV_NAME_FIELD, name);
        infoJson.put(LINE_NUMBER_FIELD, lineNumber);
        infoJson.put(ITER_ID_FIELD, iterId);
        infoJson.put(CHECK_TIME_FIELD, checkTime);
        infoJson.put(INV_RESULT_FIELD, result.toString());
        return infoJson.toJSONString();
    }

    public static CheckInfo fromString(String json) {
        JSONObject infoJson = JSONObject.parseObject(json);
        return new CheckInfo(
                infoJson.getString(INV_NAME_FIELD),
                infoJson.getInteger(LINE_NUMBER_FIELD),
                infoJson.getInteger(ITER_ID_FIELD),
                infoJson.getLong(CHECK_TIME_FIELD),
                CheckResult.fromString(infoJson.getString(INV_RESULT_FIELD)));
    }
}
